#include "stdafx.h"
#include "msg.h"

CMsgImpl::CMsgImpl(void)
{
	Clear();
}
CMsgImpl::~CMsgImpl(void)
{
	Clear();
}
std_method_impl CMsgImpl::SetMsgType(unsigned int type)
{
	m_msgtype = type;
	return S_OK;
}
std_method_type_impl(unsigned int) CMsgImpl::GetMsgType()
{
	return m_msgtype;
}
std_method_impl CMsgImpl::SetId(unsigned int id)
{
	m_id = id;
	return S_OK;
}
std_method_type_impl(unsigned int) CMsgImpl::GetId()
{
	return m_id;
}
std_method_impl CMsgImpl::SetVersion(unsigned int version)
{
	m_version = version;
	return S_OK;
}
std_method_type_impl(unsigned int) CMsgImpl::GetVersion()
{
	return m_version;
}
std_method_impl CMsgImpl::SetLength(unsigned int len)
{
	m_pbuf.dispose();
	m_pbuf = ALLOC_NEW BYTE[len + 1]();
	rc_assert(m_pbuf.m_p != NULL, E_FAIL)
	m_msglen = len;
	return S_OK;
}
std_method_type_impl(unsigned int) CMsgImpl::GetLength()
{
	return m_msglen;
}
std_method_impl CMsgImpl::SetMsgRawData(BYTE* data, unsigned int size, unsigned int offset)
{
	rc_assert(((data != NULL) && size != 0), E_FAIL)
	s_memcpy(m_pbuf.m_p + offset, data, size);
	return S_OK;
}
std_method_type_impl(BYTE*) CMsgImpl::GetMsgRawData(unsigned int offset)
{
	rc_assert(m_pbuf.m_p != NULL, NULL)
	return m_pbuf.m_p + offset;
}
std_method_impl CMsgImpl::Clear()
{
	Reset();
	return S_OK;
}
std_method_impl CMsgImpl::Attach(IMsg* pMsg)
{
	HRESULT hr = S_OK;

	hr = Clear();
	rc_assert(hr == S_OK, E_FAIL)

	hr = SetMsgType(pMsg->GetMsgType());
	rc_assert(hr == S_OK, E_FAIL)

	hr = SetId(pMsg->GetId());
	rc_assert(hr == S_OK, E_FAIL)

	hr = SetVersion(pMsg->GetVersion());
	rc_assert(hr == S_OK, E_FAIL)

	hr = SetLength(pMsg->GetLength());
	rc_assert(hr == S_OK, E_FAIL)

	hr = SetMsgRawData(pMsg->GetMsgRawData(0), pMsg->GetLength(),0);
	rc_assert(hr == S_OK, E_FAIL)

	return S_OK;
}
std_method_impl CMsgImpl::Reset()
{
	m_id = 0;
	m_version = 0;
	m_msgtype = 0;
	m_msglen = 0;
	m_pbuf.dispose();
	return S_OK;
}
